//
//  SDPhotoPreview.m
//  SKinDiary
//
//  Created by pcyang on 16/7/17.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPhotoPreview.h"
#import "SDImage.h"
#import "UIImage+FixOrientation.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface SDPhotoPreview()
@property(nonatomic,strong)SDSimpleImageView* photoView;
@property(nonatomic,strong)SDPhotoInfo* photoInfo;
@end

@implementation SDPhotoPreview
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        _photoView = [[SDSimpleImageView alloc] initWithFrame:CGRectMake(kTagLeftPadding, kTagLeftPadding, 149, kPhotoPrevViewHeight)];
        _photoView.contentMode = UIViewContentModeScaleAspectFill;
        _photoView.left = (kScreenWidth - 149)/2;
        [self.contentView addSubview:_photoView];
        
        UIView* bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(kTagLeftPadding, kPhotoCellHeight - 1, kScreenWidth - 2*kTagLeftPadding, 1)];
        [bottomLineView setBackgroundColor:[UIColor colorWithRGB:0xcad6e3]];
        [self.contentView addSubview:bottomLineView];

    }
    return self;
}

-(void)setPhotoInfo:(SDPhotoInfo *)photoInfo
{
    _photoInfo = photoInfo;
    if (CHECK_VALID_STRING(_photoInfo.originalFileName)) {
        NSString* directory = [SDFile getDirectoryInCacheWithName:kPhotoDiretoryName];
        NSString *filePath = [directory stringByAppendingPathComponent:_photoInfo.originalFileName];
        if ([SDFile fileExist:filePath]) {
            UIImage* image = [UIImage imageWithContentsOfFile:filePath];
            _photoView.image = image;
        }
    } else {
        if (CHECK_VALID_STRING(_photoInfo.photoUrl)) {
            [_photoView setImageWithURL:_photoInfo.photoUrl];
        }
    }
}
@end
